package com.example.springaopdemo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class AspectDemo {

    //前置通知
    @Before("execution(* com.example.springaopdemo.contoller.*.*(..))")
    public void doBefore(){
        log.info("执行Before");
    }

    //后置通知
    @After("execution(* com.example.springaopdemo.contoller.*.*(..))")
    public void doAfter(){
        log.info("执行After");
    }

    //返回后通知
    @AfterReturning("execution(* com.example.springaopdemo.contoller.*.*(..))")
    public void doAfterReturning(){
        log.info("执行AfterReturning");
    }

    //抛出异常后执行
    @AfterThrowing("execution(* com.example.springaopdemo.contoller.*.*(..))")
    public void doAfterThrowing(){
        log.info("执行AfterThrowing");
    }

    //环绕通知
    @Around("execution(* com.example.springaopdemo.contoller.*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) {
        log.info("Around方法开始执行");
        Object result= null;
        try {
            result = pjp.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }finally {
            log.info("Around方法执行结束");
        }

        return result;
    }

}
